/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package UserClient;

import java.awt.Toolkit;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JOptionPane;

import UserClient.PurchaseJFrame;

/**
 *
 * @author C064
 */
public class ChooseJFrame extends javax.swing.JFrame {

    /** Creates new form ChooseByRandomJFrame */
    public ChooseJFrame() {
        initComponents();
        super.setTitle("彩票购买抽奖程序");
        int width = Toolkit.getDefaultToolkit().getScreenSize().width;
        int height = Toolkit.getDefaultToolkit().getScreenSize().height;
        super.setLocation((width - 400) / 2, (height - 350) / 2);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        Num1Label = new javax.swing.JLabel();
        Num2Label = new javax.swing.JLabel();
        Num3Label = new javax.swing.JLabel();
        Num4Label = new javax.swing.JLabel();
        Num5Label = new javax.swing.JLabel();
        Num6Label = new javax.swing.JLabel();
        Num7Label = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        ReturnButton = new javax.swing.JButton();
        OKButton = new javax.swing.JButton();
        Num2Field = new javax.swing.JTextField();
        Num1Field = new javax.swing.JTextField();
        Num3Field = new javax.swing.JTextField();
        Num4Field = new javax.swing.JTextField();
        Num5Field = new javax.swing.JTextField();
        Num6Field = new javax.swing.JTextField();
        Num7Field = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        TimesField = new javax.swing.JTextField();
        BillField = new javax.swing.JTextField();
        BillButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("宋体", 1, 24)); // NOI18N
        jLabel1.setText("选择幸运号");

        Num1Label.setFont(new java.awt.Font("宋体", 1, 14)); // NOI18N
        Num1Label.setText("1");

        Num2Label.setFont(new java.awt.Font("宋体", 1, 14)); // NOI18N
        Num2Label.setText("2");

        Num3Label.setFont(new java.awt.Font("宋体", 1, 14)); // NOI18N
        Num3Label.setText("3");

        Num4Label.setFont(new java.awt.Font("宋体", 1, 14)); // NOI18N
        Num4Label.setText("4");

        Num5Label.setFont(new java.awt.Font("宋体", 1, 14)); // NOI18N
        Num5Label.setText("5");

        Num6Label.setFont(new java.awt.Font("宋体", 1, 14)); // NOI18N
        Num6Label.setText("6");

        Num7Label.setFont(new java.awt.Font("宋体", 1, 14)); // NOI18N
        Num7Label.setText("7");

        jLabel9.setText("每注2元，加倍加价！");

        ReturnButton.setText("返回");
        ReturnButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ReturnButtonActionPerformed(evt);
            }
        });

        OKButton.setText("确认购买");
        OKButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                OKButtonActionPerformed(evt);
            }
        });

        jButton1.setText("随机选号");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("仿宋", 1, 14)); // NOI18N
        jLabel2.setText("倍数");

        TimesField.setText("1");
        TimesField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TimesFieldActionPerformed(evt);
            }
        });

        BillField.setEditable(false);
        BillField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BillFieldActionPerformed(evt);
            }
        });

        BillButton1.setText("计算金额");
        BillButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BillButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(ReturnButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(OKButton)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jLabel9)
                                .addContainerGap())
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(89, 89, 89))))))
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(Num1Field, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Num1Label)
                    .addComponent(Num5Field, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Num5Label))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(Num2Field, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Num2Label)
                    .addComponent(Num6Field, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Num6Label))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(Num3Field, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Num3Label)
                    .addComponent(Num7Field, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Num7Label))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                        .addComponent(Num4Label)
                        .addComponent(Num4Field, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(TimesField, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel2)))
                .addContainerGap(42, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(BillButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(BillField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {BillButton1, BillField});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jLabel9)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Num2Field, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Num1Field, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Num3Field, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Num4Field, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Num1Label)
                    .addComponent(Num2Label)
                    .addComponent(Num3Label)
                    .addComponent(Num4Label))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Num5Field, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Num6Field, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Num7Field, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(TimesField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Num5Label)
                    .addComponent(Num6Label)
                    .addComponent(Num7Label)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 38, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BillField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(BillButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(OKButton)
                    .addComponent(ReturnButton)
                    .addComponent(jButton1))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>                        

    private void ReturnButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
        new PurchaseJFrame().setVisible(true);
        this.setVisible(false);
    }                                            

    private void OKButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
        if(BillField.getText().length() != 0){
            ArrayList<Integer> list = new ArrayList<>();
            int[] notes = new int[7];
            notes[0] = Integer.parseInt(Num1Field.getText());
            notes[1] = Integer.parseInt(Num2Field.getText());
            notes[2] = Integer.parseInt(Num3Field.getText());
            notes[3] = Integer.parseInt(Num4Field.getText());
            notes[4] = Integer.parseInt(Num5Field.getText());
            notes[5] = Integer.parseInt(Num6Field.getText());
            notes[6] = Integer.parseInt(Num7Field.getText());
            for (int i = 0; i < notes.length; i++) {
                if(notes[i]<1 || notes[i]>36){
                    JOptionPane.showMessageDialog(null, "请输入<1,36>范围的数！");
                }else if(!list.contains(notes[i])){
                    list.add(notes[i]);
                }
            }
            if(list.size() < 7){
                JOptionPane.showMessageDialog(null, "请勿输入重复数字！");
            }
            User.setNotes(notes);
            User.setNum(Integer.parseInt(TimesField.getText()));
          //-----------此处有该---------
            PreparedStatement prepare = null;
            try {
                Class.forName(driverName); // 加载MySql的驱动程序
                connection = DriverManager.getConnection(url, userName, password);
    			String strSql = "update userreserve set num1 =?,num2=?,num3=?,num4=?,num5=?,num6=?,num7=?,times=?, money=? where username= ?";
                connection.setAutoCommit(false);
                prepare = connection .prepareStatement(strSql);
                for (int i = 0; i < notes.length; i++) {
    				prepare.setInt(i+1,notes[i]);
    			}
                prepare.setInt(8, Integer.parseInt(TimesField.getText()));//设置倍数
                prepare.setString(9, BillField.getText());  //获取余额
                prepare.setString(10, User.getUsername()); 
                prepare.addBatch();
    			prepare.executeBatch();
    			connection.commit();
            }catch (Exception e) {
    			e.printStackTrace();
    		}
            //System.out.println(User.getNum());
            new PurchaseJFrame().setVisible(true);
            //----------------到这结束----------
            System.out.println(User.getNum());
            new PurchaseJFrame().setVisible(true);
            this.setVisible(false);
        } else {
            JOptionPane.showMessageDialog(null, "请先计算金额！");
        }
    }                                        

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        ArrayList<Integer> list = new ArrayList<>();
        Random random = new Random();
        int i = 0, num = 0;
        while (i < 7) {
                num = random.nextInt(35) + 1;
                if (!list.contains(num)) {
                        list.add(num);
                } else continue;
                i++;
        }
        System.out.println(list.size());

        Num1Field.setText(list.remove(0) + "");
        Num2Field.setText(list.remove(0) + "");
        Num3Field.setText(list.remove(0) + "");
        Num4Field.setText(list.remove(0) + "");
        Num5Field.setText(list.remove(0) + "");
        Num6Field.setText(list.remove(0) + "");
        Num7Field.setText(list.remove(0) + "");
    }                                        

    private void TimesFieldActionPerformed(java.awt.event.ActionEvent evt) {                                           
        
    }                                          

    private void BillFieldActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
    }                                         

    private void BillButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                            
        int times = Integer.parseInt(TimesField.getText());
        if(Num1Field.getText().length()==0 || Num2Field.getText().length()==0 || 
                Num3Field.getText().length()==0 || Num4Field.getText().length()==0 || 
                Num5Field.getText().length()==0 || Num6Field.getText().length()==0 || 
                Num7Field.getText().length()==0){
            JOptionPane.showMessageDialog(null, "请输入有效号码！");
        } else {
            int bill = 2 * times;
            User.setNum(User.getNum() + times);
            System.out.println(User.getNum());
            BillField.setText(bill + "");
        }
    }                                           

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ChooseJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ChooseJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ChooseJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ChooseJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ChooseJFrame().setVisible(true);
            }
        });
    }
    
    private String url = "jdbc:mysql://localhost:3306/message?useUnicode=true&characterEncoding=GBK&useSSL=false";
	private String userName = "root";
	private String password = "552364871";
	private String driverName = "com.mysql.jdbc.Driver";
	private static Connection connection = null;
	private static Statement statement = null;
	private static ResultSet resultSet = null;

    // Variables declaration - do not modify                     
    private javax.swing.JButton BillButton1;
    private javax.swing.JTextField BillField;
    private javax.swing.JTextField Num1Field;
    private javax.swing.JLabel Num1Label;
    private javax.swing.JTextField Num2Field;
    private javax.swing.JLabel Num2Label;
    private javax.swing.JTextField Num3Field;
    private javax.swing.JLabel Num3Label;
    private javax.swing.JTextField Num4Field;
    private javax.swing.JLabel Num4Label;
    private javax.swing.JTextField Num5Field;
    private javax.swing.JLabel Num5Label;
    private javax.swing.JTextField Num6Field;
    private javax.swing.JLabel Num6Label;
    private javax.swing.JTextField Num7Field;
    private javax.swing.JLabel Num7Label;
    private javax.swing.JButton OKButton;
    private javax.swing.JButton ReturnButton;
    private javax.swing.JTextField TimesField;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel9;
    // End of variables declaration                   

}
